package com.water_work_form.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.water_work_form.entity.BasicDevice;
import com.water_work_form.mapper.BasicDeviceMapper;
import com.water_work_form.service.BasicDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * <p>
 * 基础表——设备计量 服务实现类
 * </p>
 *
 * @author xuke
 * @since 2023-08-01
 */
@Service
public class BasicDeviceServiceImpl extends ServiceImpl<BasicDeviceMapper, BasicDevice> implements BasicDeviceService {

    @Autowired
    BasicDeviceMapper basicDeviceMapper;

    /**
     * 设备计量 查看（分页）
     * @param name 仪器名称
     * @param current 分页 当前页
     * @param size 分页 每页数据量
     * @return 返回当页数据
     */
    @Override
    public IPage<BasicDevice> listDevice(String name, Integer current, Integer size){
        QueryWrapper<BasicDevice> queryWrapper = new QueryWrapper<>();
        if(name != null && name.length() > 0){
            queryWrapper.like("name", name);
        }
        IPage<BasicDevice> page = new Page<>(current, size);
        return this.page(page, queryWrapper);
    }

    /**
     * 设备计量 判断name是否为空，为空给出device+当前日期+三位随机数的编号
     * @param basicDevice 添加的项目
     */
    @Override
    public void addName(BasicDevice basicDevice){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date currentTime = new Date();
        String currentFormattedTime = dateFormat.format(currentTime);
        Random random = new Random();
        int randomNumber = random.nextInt(900) + 100; // 生成100到999之间的随机数
        if(basicDevice.getName() == null || basicDevice.getName().trim().isEmpty()){
            basicDevice.setName("device" + currentFormattedTime + randomNumber);
        }
    }
}
